// 推荐模块相关Hook

import { useState, useCallback } from 'react';
import {
  RecommendationContent,
  ContentFeatureStatistics,
  RetryFailedTasksResponse,
  CleanupTimeoutTasksResponse,
  CleanupTimeoutParams
} from '../types/recommendation/recommendation';
import {
  getNextRecommendation,
  getStatistics,
  retryFailedTasks,
  cleanupTimeoutTasks
} from '../api/recommendation/recommendation';

/**
 * 推荐内容Hook
 */
export const useRecommendation = () => {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [currentRecommendation, setCurrentRecommendation] = useState<RecommendationContent | null>(null);

  // 获取下一个推荐内容
  const fetchNextRecommendation = useCallback(async () => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await getNextRecommendation();
      if (response.code === 200 && response.data) {
        setCurrentRecommendation(response.data);
        return response.data;
      } else {
        setError(response.message || '获取推荐内容失败');
        return null;
      }
    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : '网络错误，请稍后重试';
      setError(errorMessage);
      return null;
    } finally {
      setLoading(false);
    }
  }, []);

  return {
    loading,
    error,
    currentRecommendation,
    fetchNextRecommendation,
    clearError: () => setError(null)
  };
};

/**
 * 内容特征管理Hook（管理员功能）
 */
export const useContentFeatureAdmin = () => {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [statistics, setStatistics] = useState<ContentFeatureStatistics | null>(null);

  // 获取任务统计
  const fetchStatistics = useCallback(async () => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await getStatistics();
      if (response.code === 200 && response.data) {
        setStatistics(response.data);
        return response.data;
      } else {
        setError(response.message || '获取统计信息失败');
        return null;
      }
    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : '网络错误，请稍后重试';
      setError(errorMessage);
      return null;
    } finally {
      setLoading(false);
    }
  }, []);

  // 重试失败任务
  const handleRetryFailedTasks = useCallback(async () => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await retryFailedTasks();
      if (response.code === 200 && response.data) {
        // 重新获取统计信息
        await fetchStatistics();
        return response.data;
      } else {
        setError(response.message || '重试失败任务失败');
        return null;
      }
    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : '网络错误，请稍后重试';
      setError(errorMessage);
      return null;
    } finally {
      setLoading(false);
    }
  }, [fetchStatistics]);

  // 清理超时任务
  const handleCleanupTimeoutTasks = useCallback(async (params?: CleanupTimeoutParams) => {
    setLoading(true);
    setError(null);
    
    try {
      const response = await cleanupTimeoutTasks(params);
      if (response.code === 200 && response.data) {
        // 重新获取统计信息
        await fetchStatistics();
        return response.data;
      } else {
        setError(response.message || '清理超时任务失败');
        return null;
      }
    } catch (err) {
      const errorMessage = err instanceof Error ? err.message : '网络错误，请稍后重试';
      setError(errorMessage);
      return null;
    } finally {
      setLoading(false);
    }
  }, [fetchStatistics]);

  return {
    loading,
    error,
    statistics,
    fetchStatistics,
    retryFailedTasks: handleRetryFailedTasks,
    cleanupTimeoutTasks: handleCleanupTimeoutTasks,
    clearError: () => setError(null)
  };
}; 